﻿// Copyright: 2012 
// Author: Minh Vu - YoungJ
// File name: Utilites.cs
// Solution: WindowsPhoneRssReader
// Project: WindowsPhoneRssReader
// Time: 1:20 PM 07/05/2012

using System;
using System.Net;
using System.Xml.Linq;

namespace WindowsPhoneRssReader.Utils
{
    public static class Extension
    {
        public static string GetValue(this XElement element, string elementName)
        {
            var xElement = element.Element(elementName);
            return xElement != null ? xElement.Value : string.Empty;
        }

        public static string GetAttributeValue(this XElement element, string elementName)
        {
            var xElement = element.Attribute(elementName);
            return xElement != null ? xElement.Value : string.Empty;
        }

        public static string GetDescription(this XElement element)
        {
            var content = XNamespace.Get("http://purl.org/rss/1.0/modules/content/");
            var xElement = element.Element(content + "encoded") ?? element.Element("description");
            return xElement != null ? xElement.Value : string.Empty;
        }

        public static string UrlEncode(this string url)
        {
            return HttpUtility.UrlEncode(url);
        }
        public static string UrlDecode(this string url)
        {
            return HttpUtility.UrlDecode(url);
        }

        public static DateTime ConvertToDateTime(this string strDate)
        {
            if (string.IsNullOrEmpty(strDate)) return DateTime.Now.AddYears(-1);
            if (strDate.Contains("PDT"))
            {
                strDate = strDate.Replace("PDT", string.Empty);
            }
            DateTime dateTime;
            return DateTime.TryParse(strDate, out dateTime) ? dateTime : DateTime.Now.AddYears(-1);
        }
    }
}